package com.han.transfer.resource;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author HanJiafeng
 * @date 2021年6月8日 10点37分
 * @desc 资源载入类
 */
public abstract class ResourceManager {

    /**
     * 金额图片map
     */
    private final Map<String, BufferedImage> amountCharacterMap = new HashMap<>(12);
    private BufferedImage templateImage;
    private int templateImageWidth;

    protected void loadTemplateImage() {
        //加载模板图片
        try {
            templateImage = ImageIO.read(new File("src/main/resources/images/template.jpg"));
            templateImageWidth = templateImage.getWidth();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取指定目录中的图片，存储到map中
     *
     * @param map map
     */
    private void readImage(Map<String, BufferedImage> map) {
        File tempDir = new File("src/main/resources/images/amount_number");
        File[] tempFileList = tempDir.listFiles();
        for (File file : Objects.requireNonNull(tempFileList)) {
            try {
                String fileName = file.getName();
                BufferedImage bufferedImage = ImageIO.read(file);
                map.put(fileName, bufferedImage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    protected void loadResource() {
        //加载模板图片
        loadTemplateImage();

        //加载金额图片
        readImage(amountCharacterMap);
    }

    protected BufferedImage getTemplateImage() {
        return templateImage;
    }

    protected BufferedImage getAmountImage0() {
        return amountCharacterMap.get("0.jpg");
    }

    protected BufferedImage getAmountImage1() {
        return amountCharacterMap.get("1.jpg");
    }

    protected BufferedImage getAmountImage2() {
        return amountCharacterMap.get("2.jpg");
    }

    protected BufferedImage getAmountImage3() {
        return amountCharacterMap.get("3.jpg");
    }

    protected BufferedImage getAmountImage4() {
        return amountCharacterMap.get("4.jpg");
    }

    protected BufferedImage getAmountImage5() {
        return amountCharacterMap.get("5.jpg");
    }

    protected BufferedImage getAmountImage6() {
        return amountCharacterMap.get("6.jpg");
    }

    protected BufferedImage getAmountImage7() {
        return amountCharacterMap.get("7.jpg");
    }

    protected BufferedImage getAmountImage8() {
        return amountCharacterMap.get("8.jpg");
    }

    protected BufferedImage getAmountImage9() {
        return amountCharacterMap.get("9.jpg");
    }

    protected BufferedImage getAmountImageBar() {
        return amountCharacterMap.get("bar.jpg");
    }

    protected BufferedImage getAmountImageDot() {
        return amountCharacterMap.get("dot.jpg");
    }

    protected int getTemplateImageWidth() {
        return templateImageWidth;
    }
}
